
#include "rpucfg.h"

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <algorithm>


#if 0
int RPUConfig::createCL1File(const String &fileName){

	// Create file
	std::ofstream CL1File(fileName.c_str());

	assert(CL1File);

	const String & graphName = dfgGraph.name();

	// uppercase of name
	String GRAPHNAME = graphName;
	std::transform(GRAPHNAME.begin(), GRAPHNAME.end(), 
		GRAPHNAME.begin(), ::toupper);

	CL1File<<
	"/******************************************************\n"
	" * This file is generated by RPUCompiler automatically.\n"
	" * ----------------------------------------------------\n"
	" * This file include CL1 context to initialize GCGM. \n"
	" */\n\n"
	"#ifndef "<<GRAPHNAME<<"_CL1_H\n"
	"#define "<<GRAPHNAME<<"_CL1_H\n"<<std::endl;

	CL1File<<"\n#define "<<GRAPHNAME<<"_CL1_SIZE\t"
		<<std::dec<<CL1Context.size()<<";\n\n";

	int cl1SeqNo = 0;
	Vector<Vector<reg32> >::iterator cl1Iter;
	for(cl1Iter = CL1Context.begin(); 
		cl1Iter != CL1Context.end(); ++ cl1Iter, ++ cl1SeqNo){

		Vector<reg32> & curCL1Context = * cl1Iter;

		const String & graphName = dfgGraph.name();

		// Print context
		//---------------------------------------------------------

		std::size_t cl1Size = curCL1Context.size();

		CL1File<<"\n#define "<<GRAPHNAME<<"_CL1_"<<cl1SeqNo
			<<"_SIZE\t"<<std::dec<<cl1Size<<";\n\n";

		CL1File<<"unsigned int "<<graphName<<"_CL1_ctx_"
			<<cl1SeqNo<<" [] = {";

		CL1File.fill('0');

		std::size_t cl1size = curCL1Context.size();

		for(std::size_t i =0; i <cl1size; ++ i){

			CL1File<<"\n\t\t0x"<<std::setw(8)<<std::hex<<curCL1Context[i];

			if( i + 1 != cl1size)CL1File<<",";
		}

		CL1File<<"\n};\n\n"<<std::endl;
	}

	CL1File<<"#endif"<<std::endl;

	return 0;
}

#endif


int RPUConfig::createCL1File(const String &fileName,const String &C_fileName){

	// Create file
	std::ofstream CL1File(fileName.c_str());
	std::ofstream C_CL1File(C_fileName.c_str());

	assert(CL1File);
	assert(C_CL1File);

	//const String & graphName = dfgGraph.name();

	// uppercase of name
	//String GRAPHNAME = graphName;
	//std::transform(GRAPHNAME.begin(), GRAPHNAME.end(), 
	//	GRAPHNAME.begin(), ::toupper);

	C_CL1File<<"[Context Group Memory]\n";
	

	int cl1SeqNo = 0;
	Vector<Vector<reg32> >::iterator cl1Iter;

	std::size_t cl1SizeCount;
	cl1SizeCount=0;
	for(cl1Iter = CL1Context.begin(); cl1Iter != CL1Context.end(); ++ cl1Iter)
	{
		Vector<reg32> & curCL1Context = * cl1Iter;	
		cl1SizeCount += curCL1Context.size();
	}


	C_CL1File<<"Group Cnt="<<std::dec<<cl1SizeCount<<"\n\n";


	cl1SizeCount=0;
	for(cl1Iter = CL1Context.begin(); cl1Iter != CL1Context.end(); ++ cl1Iter, ++ cl1SeqNo)
	{

		Vector<reg32> & curCL1Context = * cl1Iter;

		const String & graphName = dfgGraph.name();

		// Print context
		//---------------------------------------------------------

		std::size_t cl1Size = curCL1Context.size();

		//CL1File<<"\n#define "<<GRAPHNAME<<"_CL1_"<<cl1SeqNo<<"_SIZE\t"<<std::dec<<cl1Size<<";\n\n";


		CL1File.fill('0');
		C_CL1File.fill('0');

		std::size_t cl1size = curCL1Context.size();

		for(std::size_t i =0; i <cl1size; ++ i)
		{
			CL1File<<std::setw(8)<<std::hex<<curCL1Context[i]<<"\n";
			C_CL1File<<"GroupWord["<<std::dec<<(i+cl1SizeCount)<<"]=0x"<<std::setw(8)<<std::hex<<curCL1Context[i]<<";\n";
		}

		cl1SizeCount +=cl1size;

	//	CL1File<<"\n\n"<<std::endl;
	}

	//CL1File<<"#endif"<<std::endl;
	CL1File.close();
	C_CL1File.close();

	return 0;
}
